<?php

namespace zhtfrontend\modules\parentsgrowth\controllers;

use common\models\datas\Customer;
use yii\console\Exception;
use yii\data\ActiveDataProvider;
use yii\db\Query;
use zhtfrontend\models\datas\Admin;
use zhtfrontend\models\datas\Comment;
use zhtfrontend\models\datas\CommentRelation;
use zhtfrontend\models\datas\Mechanic;
use zhtfrontend\models\datas\Teacher;
use zhtfrontend\modules\teachergrowth\components\rest\ActiveController;
use yii\helpers\ArrayHelper;
use yii\web\ForbiddenHttpException;
use yii\web\HttpException;
use yii\web\NotFoundHttpException;
use yii\web\Response;

class SiteController extends ActiveController
{
    public $modelClass = 'zhtfrontend\models\datas\Admin';

    public $serializer = [
        'class' => 'zhtfrontend\modules\teachergrowth\components\rest\Serializer',
        'collectionEnvelope' => 'items',
    ];

    public function behaviors()
    {
        $behaviors = parent::behaviors();
        $behaviors['contentNegotiator']['formats']['text/html'] = Response::FORMAT_JSON;
        return $behaviors;
    }
    public function actions()
    {
        $actions = parent::actions();
        unset($actions['index'], $actions['create'], $actions['update'], $actions['delete'], $actions['view']);
        return $actions;
    }

    public function actionIndex(){

    }

    public function actionWechatlogin(){
        $data=\Yii::$app->request->post();
        $core=ArrayHelper::keyExists('code', $data) ? $data['code'] : '';
        $appid='wxa62ed9473d66589a';
        $secret='843ec3b4b7ab0f9b32ec88b00713aa6a';
        $json = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$appid.'&secret='.$secret.'&js_code='.$core.'&grant_type=authorization_code';
        header("Content-Type: application/json");
        $response=file_get_contents($json);//获取openid,session_key等信息
        var_dump($response);die;
        return ['statusCode' => 200, 'message' => 'success', 'items' => '请求成功'];
    }

    private function generateCode()
    {
        return rand(1000, 9999);
    }

    public function actionGetcode(){
        $data=\Yii::$app->request->post();
        $telephone=ArrayHelper::keyExists('telephone', $data) ? $data['telephone'] : '';
        $code = $this->generateCode();
        //$cur_session = \Yii::$app->session;
        //$cur_session->setTimeout(1800);//设置session的保存时间为5分钟，好像没有生效
        //$cur_session->set('app_sms_code', '121212');
        \Yii::$app->cache->set('smscode_'.$telephone, $code, 3600 * 24);
        $res['mobile'] = $telephone;
        $res['content'] = "您的验证码是：【" . $code . "】。请不要把验证码泄露给其他人。";
        \Yii::$app->smsSend->send($res);
        return ['statusCode' => 200, 'message' => 'error', 'items' => $code];
    }


    public function actionCodelogin(){
        $data=\Yii::$app->request->post();
        $telephone=ArrayHelper::keyExists('telephone', $data) ? $data['telephone'] : '';
        $code=ArrayHelper::keyExists('code', $data) ? $data['code'] : '';
        $session_code=\Yii::$app->cache->get('smscode_'.$telephone);
        $model = Customer::find()->where(['mobile'=>$telephone])->one();
        if (empty($model)) {
            try {
                throw new NotFoundHttpException('该用户不存在');
            } catch (NotFoundHttpException $e) {
                return ['statusCode' => 0, 'message' => 'error', 'result' => '该用户不存在'];
            }
        }else{
            if ($session_code!=$code){
                return ['statusCode' => 0, 'message' => 'error', 'result' => '验证码不正确'];
            }else{
                return ['statusCode' => 200, 'message' => 'error', 'items' => $model];
            }
        }

    }

    public function actionGrowthindex(){
        $data=\Yii::$app->request->post();
        $customer_id=ArrayHelper::keyExists('customer_id', $data) ? $data['customer_id'] : '';
        $customer = (new Query())->select('*')->from('{{%customer}}')->where(['id' => $customer_id])->one();
        if (empty($customer)){
            return ['statusCode' => 0, 'message' => 'error', 'result' => '用户不存在'];
        }else{
            $items=[
                'customer_info'=>$customer,
            ];
            return ['statusCode' => 200, 'message' => 'error', 'items' => $items];
        }
    }



    public function actionStudentlist(){

        $data=\Yii::$app->request->post();
        $customer_id=ArrayHelper::keyExists('customer_id', $data) ? $data['customer_id'] : '';
        $students=(new Query())->select('*')->from('{{%student}}')->where(['customer_id' => $customer_id])->all();
        if(empty($students)){
            return ['statusCode' => 0, 'message' => 'error', 'result' => '暂时没有学生信息'];
        }else{
            $student_info=[];
            foreach ($students as $key=>$val){
                $sex=$val['sex'];
                if ($sex==0){//0是女生
                    $sexiconBox='dmd-female-icon';
                    $sexIcon='icon-female';
                }else{
                    $sexiconBox='dmd-male-icon';
                    $sexIcon='icon-male';
                }
                $student_info[$key]=[
                    'id'=>$val['id'],
                    'name'=>$val['name'],
                    'nickname'=>$val['nickname'],
                    'sex'=>$val['sex'],
                    'sexiconBox'=>$sexiconBox,
                    'sexIcon'=>$sexIcon,
                    'img'=>$val['img'],
                ];
            }
            $items=[
                'student_info'=>$student_info,
            ];
            return ['statusCode' => 200, 'message' => 'error', 'items' => $items];
        }
    }


}